//
//  LLBUtil.swift
//  LLBUtil
//
//  Created by Yun on 2018/11/2.
//

import UIKit

///返回字体，如果自定义字体可以在这里区分
public func llbFont(size:CGFloat) ->UIFont{
    return UIFont.systemFont(ofSize: size.llbWF)
}
///粗体
public func llbMediumFont(size:CGFloat) -> UIFont{
    return UIFont.boldSystemFont(ofSize: size.llbWF)
}

/// 获取顶层控制器
public func getCurrentController() -> UIViewController? {
    let vc = UIApplication.shared.keyWindow?.rootViewController
    return findBestViewController(vc: vc)
}

func findBestViewController(vc : UIViewController?) -> UIViewController? {
    if let _ = vc {
        if vc!.presentedViewController != nil {
            return findBestViewController(vc: vc!.presentedViewController!)
        } else if vc!.isKind(of:UISplitViewController.self) {
            let svc = vc! as! UISplitViewController
            if svc.viewControllers.count > 0 {
                return findBestViewController(vc: svc.viewControllers.last)
            } else {
                return vc
            }
        } else if vc!.isKind(of: UINavigationController.self) {
            let nvc = vc as! UINavigationController
            if nvc.viewControllers.count > 0 {
                return findBestViewController(vc: nvc.topViewController)
            } else {
                return vc
            }
        } else if vc!.isKind(of: UITabBarController.self) {
            let tvc = vc as! UITabBarController
            if (tvc.viewControllers?.count)! > 0 {
                return findBestViewController(vc: tvc.selectedViewController)
            } else {
                return vc
            }
        } else {
            return vc
        }
    }
    else{
        return nil
    }
    
}

// MARK: - Log
/// 打印输出
///
/// - Parameters:
///   - items: 打印条目
///   - file: 文件名
///   - function: 方法名
///   - line: 行数
public func DBLog(_ items: Any..., file: String = #file, function: String = #function, line: Int = #line) {
    #if DEBUG
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
    let time = formatter.string(from: Date())
    let array = file.components(separatedBy: "/");
    print("\(time) [\(String(describing: array.last!))] [\(function)] [Line \(line)]", items);
    #endif
}
